package com.qixin.modules.rzInvoice.service.impl;

import com.qixin.modules.rzInvoice.entity.EaRzZzs;
import com.qixin.modules.rzInvoice.entity.EaRzZzsItems;
import com.qixin.modules.rzInvoice.mapper.EaRzZzsItemsMapper;
import com.qixin.modules.rzInvoice.mapper.EaRzZzsMapper;
import com.qixin.modules.rzInvoice.service.IEaRzZzsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 增值税发票
 * @Author: qixin-boot
 * @Date:   2021-07-08
 * @Version: V1.0
 */
@Service
public class EaRzZzsServiceImpl extends ServiceImpl<EaRzZzsMapper, EaRzZzs> implements IEaRzZzsService {

	@Autowired
	private EaRzZzsMapper eaRzZzsMapper;
	@Autowired
	private EaRzZzsItemsMapper eaRzZzsItemsMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRzZzs eaRzZzs, List<EaRzZzsItems> eaRzZzsItemsList) {
		eaRzZzsMapper.insert(eaRzZzs);
		if(eaRzZzsItemsList!=null && eaRzZzsItemsList.size()>0) {
			for(EaRzZzsItems entity:eaRzZzsItemsList) {
				//外键设置
				entity.setRzZzsId(eaRzZzs.getId());
				eaRzZzsItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaRzZzs eaRzZzs,List<EaRzZzsItems> eaRzZzsItemsList) {
		eaRzZzsMapper.updateById(eaRzZzs);
		
		//1.先删除子表数据
		eaRzZzsItemsMapper.deleteByMainId(eaRzZzs.getId());
		
		//2.子表数据重新插入
		if(eaRzZzsItemsList!=null && eaRzZzsItemsList.size()>0) {
			for(EaRzZzsItems entity:eaRzZzsItemsList) {
				//外键设置
				entity.setRzZzsId(eaRzZzs.getId());
				eaRzZzsItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaRzZzsItemsMapper.deleteByMainId(id);
		eaRzZzsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaRzZzsItemsMapper.deleteByMainId(id.toString());
			eaRzZzsMapper.deleteById(id);
		}
	}
	
}
